package school

import (
	"ser1/database"
	"ser1/utils"
	"time"

	"gorm.io/gorm"
)

// Question 问题模型
type Question struct {
	gorm.Model

	Title      string `json:"title" gorm:"column:title;size:255;not null" example:"问题标题"`
	Content    string `json:"content" gorm:"column:content;type:longtext;not null" example:"问题内容"`
	Answer     string `json:"answer" gorm:"column:answer;type:text;not null" example:"问题答案"`
	Category   string `json:"category" gorm:"column:category;size:50;not null;index" example:"数学"`
	Difficulty int    `json:"difficulty" gorm:"column:difficulty;not null;default:1" example:"1"`
	Status     int    `json:"status" gorm:"column:status;not null;default:1" example:"1"`
	GroupId    uint   `json:"group_id" gorm:"column:group_id;not null;index" example:"1"`
	Tags       string `json:"tags" gorm:"column:tags;type:varchar(255);not null" example:"数学,基础"`
	Subject    string `json:"subject" gorm:"column:subject;not null;index" example:"1"`
	Type       string   
}

type QuestionDto struct {
	Title      string      `json:"title" example:"问题标题"`
	Content    interface{} `json:"content" example:"问题内容"`
	Answer     string      `json:"answer" example:"问题答案"`
	Category   string      `json:"category" example:"数学"`
	Difficulty int         `json:"difficulty" example:"1"`
	GroupId    uint        `json:"group_id" example:"1"`
	Tags       string      `json:"tags" example:"数学,基础"`
}

func (q Question) toDto() QuestionDto {
	return QuestionDto{
		Title:      q.Title,
		Content:    utils.ToInterface(q.Content),
		Answer:     q.Answer,
		Category:   q.Category,
		Difficulty: q.Difficulty,
		GroupId:    q.GroupId,
		Tags:       q.Tags,
	}
}

// 将方法改为普通函数，因为不能在切片类型上定义方法
func (Question) toDtoList(questions []Question) []QuestionDto {
	var dtoList []QuestionDto
	for _, question := range questions {
		dtoList = append(dtoList, question.toDto())
	}
	return dtoList
}

func (q QuestionDto) toModel() Question {
	return Question{
		Title:      q.Title,
		Content:    utils.ToJson(q.Content),
		Answer:     q.Answer,
		Category:   q.Category,
		Difficulty: q.Difficulty,
		GroupId:    q.GroupId,
		Tags:       q.Tags,
	}
}

// Answer 答题记录模型
type Answer struct {
	gorm.Model

	QuestionID uint   `json:"question_id" gorm:"column:question_id;not null;index" example:"1"`
	UserID     uint   `json:"user_id" gorm:"column:user_id;not null;index" example:"1"`
	Content    string `json:"content" gorm:"column:content;type:text;not null" example:"我的答案"`
	IsCorrect  bool   `json:"is_correct" gorm:"column:is_correct;not null;default:false" example:"true"`
	Score      int    `json:"score" gorm:"column:score;not null;default:0" example:"10"`
	GroupId    uint   `json:"group_id" gorm:"column:group_id;not null;index" example:"1"`
}

// Exam 测验模型
type Exam struct {
	gorm.Model

	Title       string    `json:"title" gorm:"column:title;size:255;not null" example:"期中考试"`
	Description string    `json:"description" gorm:"column:description;type:text" example:"数学期中考试"`
	StartTime   time.Time `json:"start_time" gorm:"column:start_time;not null" example:"2023-01-01T09:00:00Z"`
	EndTime     time.Time `json:"end_time" gorm:"column:end_time;not null" example:"2023-01-01T11:00:00Z"`
	Duration    int       `json:"duration" gorm:"column:duration;not null" example:"120"` // 考试时长(分钟)
	Status      int       `json:"status" gorm:"column:status;not null;default:1" example:"1"`
}

// QuestionGroup 问题分组模型
type QuestionGroup struct {
	gorm.Model

	Name        string `json:"name" gorm:"column:name;size:100;not null" example:"数学基础题目组"`
	Description string `json:"description" gorm:"column:description;type:text" example:"包含基础数学题目的分组"`
	Status      int    `json:"status" gorm:"column:status;not null;default:1" example:"1"`
}

// AnswerGroup 答题分组模型，包含用户ID
type AnswerGroup struct {
	gorm.Model

	Name       string    `json:"name" gorm:"column:name;size:100;not null" example:"第一组答题"`
	UserID     uint      `json:"user_id" gorm:"column:user_id;not null;index" example:"1"`
	Status     int       `json:"status" gorm:"column:status;not null;default:1" example:"1"`
	Expiration time.Time `json:"expiration" gorm:"column:expiration;not null" example:"2023-01-01T11:00:00Z"`
	Resoult    string    `json:"resoult" gorm:"column:resoult;type:text" example:"[1,3,2,1,4,5,6,7,8,9,10]"`
}

func DB1() *gorm.DB {
	return database.GetDB()
}

func init() {
	database.AutoMigrateTables(&Question{}, &Answer{}, &Exam{}, &QuestionGroup{}, &AnswerGroup{})
}
